package org.example.JUC.AQS;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.Condition;

/**
 * @description:
 * @author: farprt
 * @create: 2022-08-22 15:19
 **/
public class CustomerAndProducer {

    final static NonReentrantLock lock=new NonReentrantLock();
    final static Condition notFull=lock.newCondition();
    final static Condition notEmpty=lock.newCondition();
    final static Queue<String> queue=new LinkedBlockingDeque<>();
    final static int queueSize=10;

    public static void main(String[] args) {
        Thread producer=new Thread(()->{
            lock.lock();
            try{
                while(queue.size()==queueSize){
                    notEmpty.await();
                }
                queue.add("ele");
                //唤醒消费线程
                notFull.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        });

        Thread consumer =new Thread(()->{
                lock.lock();
                while (0==queue.size()){
                    try {
                        notFull.await();
                        String ele=queue.poll();
                        notEmpty.signalAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                }
        });

        producer.start();
        consumer.start();
    }




}